package com.audaque.encoding.controller;

import com.audaque.encoding.coding.CodingTask;
import com.audaque.encoding.coding.CodingV3Service;
import com.audaque.encoding.dto.CallbackDTO;
import com.audaque.encoding.util.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author mayong
 * @since 2024/7/1
 */

@Slf4j
@RestController
@Tag(name = "V3.0 赋码请求", description = "V3.0 赋码相关的接口")
public class V3Controller {


    @Resource
    private CodingV3Service codingV3Service;

    @Resource
    private CodingTask codingTask;

    /*
    2.0逻辑: 赋码请求的源数据来自于Hive数据库，第三方赋码结束后，会将赋码的结果表写入Hive数据库
    3.0逻辑: 赋码请求的源数据来源于PostgreSQL数据库，第三方赋码结束后，会将赋码的结果回写到源数据表的某个字段中
    期望: 3.0和2.0逻辑共存，后期实现
     */

    @GetMapping("v3/code/now")
    @Operation(summary = "立即执行全库扫描，并赋码请求")
    public void startNow() {
        codingV3Service.startNow();
    }

    @GetMapping("v3/code/init-cron")
    @Operation(summary = "初始化调度配置，修改了调度表后需要调用此接口")
    public void initCron() {
        codingTask.startInit();
    }

    @PostMapping("code/call-back")
    @Operation(summary = "赋码回调接口")
    public void call(@RequestBody CallbackDTO dto) {
        log.info("------------------------------接收到赋码成功的回调信息----------------------------------");
        log.info("/n " + JsonUtil.json(dto));
        synchronized (V3Controller.class) {
            codingV3Service.handleCallback(dto);
        }
    }

    @PostMapping("v3/re-analyze")
    @Operation(summary = "失败的情况下用，重新解析Excel文件")
    public void reAnalyze(@RequestParam("id") @Parameter(description = "t_handle_callback表的id") String id) {
        codingV3Service.reAnalyze(id);
    }


}
